package com.xiaodou.mjf.application.fee.service.impl;

import com.xiaodou.mjf.application.fee.repository.AccountFeeRepository;
import com.xiaodou.mjf.application.fee.service.AccountFeeService;
import com.xiaodou.mjf.entity.fee.AccountFee;
import com.xiaodou.mjf.infrastructure.common.app.JpaSimpleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;
import java.util.List;

/**
 * AccountFee业务访问实现
 *
 * @author XiaoDou.com Code Generator
 * @version 1.0 2014-10-29 17:37:52
 */
@Service
@Transactional
public class AccountFeeServiceImpl extends JpaSimpleService<AccountFee, String>
        implements AccountFeeService {

    Logger logger = LoggerFactory.getLogger(AccountFeeServiceImpl.class);

    @Resource
    AccountFeeRepository accountFeeRepository;

    @Override
    protected JpaRepository<AccountFee, String> getRepository() {
        return accountFeeRepository;
    }

//	@Override
//	public Page<AccountFee> page(AccountFee entity) {
//		return accountFeeRepository.findAll(
//				(root, query, cb) -> {
//					Predicate p1 = cb.lessThan(
//							root.get("stageEndTime").as(Date.class),
//							entity.getStageEndTime());
//					Predicate p2 = cb.greaterThan(
//							root.get("arrearsAmount").as(Double.class),
//							entity.getArrearsAmount());
//
//					return query.where(p1, p2).getRestriction();
//				},
//				preparePage(entity, new Sort(new Sort.Order(Sort.Direction.DESC,
//						"arrearsAmount"))));
//	}

    @Override
    public Page<AccountFee> page(AccountFee entity) {
        return accountFeeRepository.findAll(
                (root, query, cb) -> {
                    Predicate p1 = cb.equal(
                            root.get("repaymentStatus").as(Integer.class),
                            entity.getRepaymentStatus());
                    Predicate p2 = cb.like(
                            root.get("memberId").as(String.class),
                            "%" + entity.getMemberId() + "%");

                    return query.where(p1, p2).getRestriction();
                },
                preparePage(entity, new Sort(new Sort.Order(Sort.Direction.DESC,
                        "arrearsAmount"))));
    }


    @Override
    public List<AccountFee> findByMemberIdAndYearAndMonth(String memberId, int year, int month, int repaymentStatus) {
        return accountFeeRepository.findByMemberIdAndYearAndMonth(memberId, year, month, repaymentStatus);
    }
    
    @Override
    public List<AccountFee> findByMemberIdAndYearAndMonth(String memberId, int year, int month) {
        return accountFeeRepository.findByMemberIdAndYearAndMonth(memberId, year, month);
    }

    @Override
    public Double sumArrearsAmount(String memberId, int year, int month) {

        return accountFeeRepository.sumArrearsAmount(memberId, year, month);
    }

    @Override
    public Integer sumTerm(String accoutNumber, int repaymentStatus,String memberId) {

        return accountFeeRepository.sumTerm(accoutNumber, repaymentStatus,memberId);
    }

}
